package com.exam.ch8;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;

public class TableModel1 {

	public TableModel1(){
		
		JFrame jFrame = new JFrame();
		
		
		JTable table = new JTable(new MyTableModel());
		table.setPreferredScrollableViewportSize(new Dimension(550, 30));//设置表格的窗口大小
		
		JScrollPane scrollPane = new JScrollPane(table);
		jFrame.getContentPane().add(scrollPane,BorderLayout.CENTER);
		
		jFrame.setTitle("Simple Table");
		jFrame.pack();
		jFrame.setVisible(true);
		jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		
	}
	
	class MyTableModel extends AbstractTableModel{

		
		private static final long serialVersionUID = -2148751179848066249L;
		Object [][] payerInfo = {{"阿呆",new Integer(66),new Integer(32),new Integer(98),new Boolean(false)},
				{"阿瓜",new Integer(85),new Integer(69),new Integer(154),new Boolean(true)}};
		String [] names = {"姓名","语文","数学","总分","及格"};
		
		@Override
		public int getRowCount() {
		
			return payerInfo.length;
		}

		@Override
		public int getColumnCount() {
	
			return names.length;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return payerInfo[rowIndex][columnIndex];
		}
		
		@Override
		public String getColumnName(int column) {
			return names[column];
		}
		
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			//返回列的数据类型
			return getValueAt(0, columnIndex).getClass();
		}
		
	}
	
	public static void main(String[] args) {
		
		new TableModel1();
		
	}
}
